/**
 * 跳转管理
 */
import {useRouter} from 'vue-router';
import {queryToJson} from '@/lib/tools.js';

const prefix = process.env.VUE_APP_PATH_URL;
const WX_PATH = process.env.VUE_APP_WX_URL;
const wxAppid = 'wx6cde7c8c03a58924';

const host = window.location.protocol + '//' + window.location.host + prefix;

const transfer = {
    /**
     * 注册路由，建议在App.vue的setup中去注册下
     */
    registerRouter() {
        this.router = useRouter();
    },

    /**
     *
     * @param {*} options
     * @param {*} isBlank
     * @param {*} needLogin
     */
    pageBack() {
        this.router.back();
    },
    /**
     * 页面跳转，所有的跳转，都通过此方法包括路由跳转
     * @param {String, Object} options
     * options 实例1，路由对象:
     * {
     *      name: 'routeName',
     *      params: {
     *          type: 1
     *      },
     *      query: {
     *          passtype: 1
     *      },
     *      method: 'replace' // 默认 'push', 可不传
     * }
     * options 实例2，不同域名 : 'http://www.3ren.cn/xxxx'
     * options 实例3，同域名 : '/xxx/xxx';
     * @param {Boolean} isBlank 是否新页面打开。true 需要，false不需要
     * @param {Boolean} needLogin 是否需要登录。true 需要，false不需要
     */
    pageJump(options, isBlank = false, needLogin = true) {
        let router = this.router;
        if (!router) {
            throw new Error('in "pageJump" function the router is undefined');
        }
        let type = typeof options;
        let url = '';
        if (isBlank) {
            if (type === 'object') {
                try {
                    url = router.resolve(options).href;
                } catch (err) {
                    console.error(options, ' 路由无效, 错误为：', err);
                }
            } else if (type === 'string') {
                url = options;
            }
            if (url && url !== '/') {
                window.open(url, isBlank ? '_blank' : '_self');
            }
        } else {
            if (type === 'object') {
                router[options.method || 'push'](options);
            } else {
                window.location.href = options;
            }
        }
    },
    pageWxAuthUrl() {
        const queries = queryToJson(window.location.href) || {};
        // 微信授权登录成功的url跳转地址
        const nextUrl = queries.backUrl && encodeURIComponent(queries.backUrl) || '';
        // 微信授权需要重定向的地址
        const wxUrl = encodeURIComponent(`${host}/wxauth?nextUrl=${nextUrl}`);
        // 微信中转地址
        let reUrl = `${WX_PATH}/ucn/static/wechat/auth.htm?wxUrl=${encodeURIComponent(wxUrl)}`;
        console.log('=======reUrl', reUrl);
        const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${wxAppid}&redirect_uri=${reUrl}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`;
        console.log('=======url', url);
        window.location.replace(url);
    }
};

let {pageJump, pageBack, pageWxAuthUrl} = transfer;
pageJump = pageJump.bind(transfer);
pageBack = pageBack.bind(transfer);
export {
    pageJump,
    pageBack,
    pageWxAuthUrl
};

export default transfer;